package cn.nuc.wwb_1248.controller;

        import org.springframework.stereotype.Controller;
        import org.springframework.util.FileCopyUtils;
        import org.springframework.web.bind.annotation.*;
        import org.springframework.web.multipart.MultipartFile;

        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.text.SimpleDateFormat;
        import java.util.Date;
        import java.util.HashMap;
        import java.util.Map;

@Controller
@RequestMapping("/wwbFile")
public class WwbFileController {
    //打开文件上传请求页面 返回指定的jsp字符串
    @GetMapping("upload")
    public String uploadPage(){
        return "wwbFile/wwbUploadOne";
    }
    @PostMapping("/upload/wwbMultipart")
    @ResponseBody
//    使用Spring MVC的MultipartFile类作为参数，对应了保存的临时文件
    public Map<String,Object> upload(@RequestParam("wwbCover") MultipartFile photo1){
        String path = "E:\\idea2019.2\\springbootspace\\wwb_1243\\src\\main\\webapp\\img";
        String fileNmae = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        System.out.println(photo1.getOriginalFilename());
        String suffix = photo1.getOriginalFilename().substring(photo1.getOriginalFilename().lastIndexOf("."));
        System.out.println(suffix);
        if (suffix.equals(".jpg")||suffix.equals(".png")){
            try {
                //Spring提供了文件操作类FileCopyUtils
                FileCopyUtils.copy(photo1.getInputStream(),new FileOutputStream(path+fileNmae+suffix));
            }catch (IOException e){
                e.printStackTrace();
                return dealResultMap(false,"上传失败");
            }
            return dealResultMap(true,"上传成功");
        }else {
            return dealResultMap(false, "上传失败");
        }
    }

    @GetMapping("/uploads")
    public String uploadPages(){
        return "/wwbFile/wwbUploads";
    }
    @PostMapping("/upload/wwbMultiparts")
    @ResponseBody
    //使用Spring MVC的MultipartFile类作为参数，对应了保存的临时文件
    public Map<String,Object> uploads(@RequestParam("photo") MultipartFile [] photos){
        String path = "E:/upload/";
        System.out.println(photos.length);
        for (MultipartFile photo: photos){
            if (!photo.isEmpty()){
                String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
                String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
                if (!suffix.equals(".jpg")&&!suffix.equals(".png")){
                    return dealResultMap(false,"上传失败");
                }
                try {
                    FileCopyUtils.copy(photo.getInputStream(),new FileOutputStream(path+fileName+suffix));
                }catch (IOException e) {
                    e.printStackTrace();
                    return dealResultMap(false, "上传失败");
                }
            }
        }
        return dealResultMap(true,"上传成功");
    }


    //保存文件
    private Map<String,Object> dealResultMap(boolean success,String msg){
        Map<String,Object> result = new HashMap<>();
        result.put("result",success);
        result.put("msg",msg);
        return result;
    }
}
